﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_project_bulletin : System.Web.UI.Page
{

    public string projectid;
    public string strName1, strContent1, strName, strContent;
    protected void Page_Load(object sender, EventArgs e)
    {

        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            string strName, strContent;
            projectid = SysFun.IsSQL(SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["projectid"])));
            if (!this.IsPostBack)
            {
                if (projectid != "" && projectid != null)
                {
                    string sql = " select title,ProjectBeginDate,ProjectEndDate,BidDate,Content from project_bulletin ";
                    sql += " WHERE projectid = " + projectid;
                    Db db = new Db();
                    System.Data.IDataReader dr;
                    dr = db.DataReader(sql);

                    if (dr.Read())
                    {
                        //txtTitle.Text = SysFun.ToTrim(dr["title"]);
                        txtProjectBeginDate.Text = Convert.ToDateTime(dr["ProjectBeginDate"]).ToShortDateString();
                        txtProjectEndDate.Text = Convert.ToDateTime(dr["ProjectEndDate"]).ToShortDateString();
                        txtBidDate.Text = Convert.ToDateTime(dr["BidDate"]).ToShortDateString();
                        //txtContent.Text = SysFun.ToTrim(dr["Content"]);
                        strName1 = SysFun.ToTrim(dr["title"]);
                        strContent1 = SysFun.ToTrim(dr["Content"]);

                    }
                    dr.Close();
                    dr.Dispose();
                    db.Close();
                }

            }
            strName = SysFun.ToTrim(Request.Form["txtName"]);
            if (strName != "")
                strName1 = strName;
            strContent = SysFun.ToTrim(Request.Form["txtContentHtml"]);
            if (strContent != "")
                strContent1 = strContent;
            if (strName != "" && strContent != "")
            {
                Db db1 = new Db();
                bool blnflag = false;
                System.Data.IDataReader dr;

                string sql = " select title,ProjectBeginDate,ProjectEndDate,BidDate,Content from project_bulletin ";
                sql += " WHERE projectid = " + projectid;

                dr = db1.DataReader(sql);

                if (dr.Read())
                {
                    blnflag = true;
                }

                dr.Close();
                dr.Dispose();
                db1.Close();

                if (blnflag)
                    Update(strName, strContent);
                else
                    Add(strName, strContent);
            }
        }
    }
    protected void btnbaocun_Click(object sender, EventArgs e)
    {
        Db db = new Db();
        bool blnflag = false;
        System.Data.IDataReader dr;

        string sql = " select title,ProjectBeginDate,ProjectEndDate,BidDate,Content from project_bulletin ";
        sql += " WHERE projectid = " + projectid;

        dr = db.DataReader(sql);

        if (dr.Read())
        {
            blnflag = true;
        }

        dr.Close();
        dr.Dispose();
        db.Close();

        if (blnflag)
            Update(strName, strContent);
        else
            Add(strName, strContent);
    }

    public void Add(string strName, string strContent)
    {
        Db db = new Db();

        //Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
        HZ.MBSM.BLL.Project_BulletinAdd project_bulletinAdd1 = new HZ.MBSM.BLL.Project_BulletinAdd();
        HZ.MBSM.Model.project_bulletin project_bulletin = new HZ.MBSM.Model.project_bulletin();
        project_bulletin.ProjectId = SysFun.ToLong(projectid);
        project_bulletin.Title = strName;
        project_bulletin.ProjectBeginDate = Convert.ToDateTime(txtProjectBeginDate.Text);
        project_bulletin.ProjectEndDate = Convert.ToDateTime(txtProjectEndDate.Text);
        project_bulletin.BidDate = Convert.ToDateTime(txtBidDate.Text);
        project_bulletin.Content = strContent.Replace("\r\n", "");
        ;
        if (project_bulletinAdd1.Add(project_bulletin) == true)
        {
            HZ.MBSM.BLL.Project UpdateProject = new HZ.MBSM.BLL.Project();
            if (UpdateProject.SetProjectHabitus("1", projectid) == true)
            {
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "project_bulletin", "project_bulletin" + project_bulletin.ProjectId + "|" + project_bulletin.Title + "|" + project_bulletin.Content, "../Agency/project_bulletin.aspx");

                Response.Write("<script>alert('保存成功！！！');</script>");
            }
        }
        db.Close();
    }

    public void Update(string strName, string strContent)
    {
        Db db = new Db();
        Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
        HZ.MBSM.Model.project_bulletin project_bulletin = (HZ.MBSM.Model.project_bulletin)oo.Retrieve(typeof(HZ.MBSM.Model.project_bulletin), projectid);
        project_bulletin.ProjectId = SysFun.ToLong(projectid);
        project_bulletin.Title = strName;
        project_bulletin.ProjectBeginDate = Convert.ToDateTime(txtProjectBeginDate.Text);
        project_bulletin.ProjectEndDate = Convert.ToDateTime(txtProjectEndDate.Text);
        project_bulletin.BidDate = Convert.ToDateTime(txtBidDate.Text);
        project_bulletin.Content = strContent.Replace("\r\n", "");
        oo.Update(project_bulletin);
        oo.Dispose();
        //Db db = new Db();
        //string strSql = "";

        //strSql = "  UPDATE project_bulletin ";
        //strSql += " SET ";
        //strSql += "Title = '" + strName + "',";
        //strSql += "ProjectBeginDate ='" + SysFun.ToTrim(txtProjectBeginDate.Text) + "',";
        //strSql += "ProjectEndDate = '" + SysFun.ToTrim(txtProjectEndDate.Text) + "',";
        //strSql += "BidDate = '" + SysFun.ToTrim(txtBidDate.Text) + "',";
        //strSql += "Content ='" + strContent + "'";//最后没有逗号，表示插入数据完成。

        //strSql += " WHERE projectid = " + SysFun.ToTrim(projectid);

        //db.Command(strSql);
        db.Close();
    }

}
